package com.ruoyi.violate.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.violate.domain.ViolateTilte;
import com.ruoyi.violate.service.IViolateTilteService;
import lombok.extern.log4j.Log4j;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.violate.domain.ViolateFile;
import com.ruoyi.violate.service.IViolateFileService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文件上传Controller
 * 
 * @author 谭拓宇
 * @date 2022-12-03
 */
@RestController
@RequestMapping("/violate/file")
public class ViolateFileController extends BaseController
{
    @Autowired
    private IViolateFileService violateFileService;

    @Autowired
    private IViolateTilteService violateTilteService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询文件上传列表
     */
    @PreAuthorize("@ss.hasPermi('violate:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(ViolateFile violateFile)
    {
        startPage();
        List<ViolateFile> list = violateFileService.selectViolateFileList(violateFile);
        Iterator var3 = list.iterator();
        while(var3.hasNext()) {
            ViolateFile entity = (ViolateFile)var3.next();
            String filter = (String)redisCache.getCacheObject("file_filter:" + entity.getId());
            if (StringUtils.isNotEmpty(filter)) {
                entity.setFilter(filter);
            }
        }

        return getDataTable(list);
    }

    /**
     * 导出文件上传列表
     */
    @PreAuthorize("@ss.hasPermi('violate:file:export')")
    @Log(title = "文件上传", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ViolateFile violateFile)
    {
        ViolateTilte tilte = new ViolateTilte();
        tilte.setTilte("（示例）万向折叠伸缩反光镜反照镜内窥镜检查镜/检测镜/焊接效果检测器");
        tilte.setUrl("（示例）https://item.taobao.com/item.htm?id=541787808780");
        List<ViolateTilte> list = new ArrayList<>();
        list.add(tilte);
        ExcelUtil<ViolateTilte> util = new ExcelUtil<ViolateTilte>(ViolateTilte.class);
        util.exportExcel(response, list, "模板");
    }

    /**
     * 获取文件上传详细信息
     */
    @PreAuthorize("@ss.hasPermi('violate:file:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(violateFileService.selectViolateFileById(id));
    }

    /**
     * 新增文件上传
     */
    @PreAuthorize("@ss.hasPermi('violate:file:add')")
    @Log(title = "文件上传", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ViolateFile violateFile)
    {
        logger.info("上传文件测试");
        if(StringUtils.isEmpty(violateFile.getFilePath()))
            return AjaxResult.error("请上传文件");
        //新增文件上传
        int insertViolateFile = violateFileService.insertViolateFile(violateFile);
        AjaxResult ajaxResult = toAjax(insertViolateFile);
        return ajaxResult ;
    }



    /**
     * 修改文件上传
     */
    @PreAuthorize("@ss.hasPermi('violate:file:edit')")
    @Log(title = "文件上传", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ViolateFile violateFile)
    {
        return toAjax(violateFileService.updateViolateFile(violateFile));
    }

    /**
     * 重新加载
     */
    @PreAuthorize("@ss.hasPermi('violate:file:cxjz')")
    @PostMapping({"/cxjx/{id}"})
    public AjaxResult cxjz(@PathVariable("id") String id)
    {
        this.violateFileService.cxjz(id);
        return AjaxResult.success();
    }


    /**
     * 删除文件上传
     */
    @PreAuthorize("@ss.hasPermi('violate:file:remove')")
    @Log(title = "文件上传", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(violateFileService.deleteViolateFileByIds(ids));
    }

    /**
     * 文件下载
     */
    @RequestMapping("/exportToId/{id}")
    public void exportToId(@PathVariable("id") String id, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {
//        //获取文件对象
//        ViolateFile entity = violateFileService.selectViolateFileById(id);
//        //生成tiltte数据对象
//        ViolateTilte violateTilte = new ViolateTilte();
//        violateTilte.setFileId(entity.getId());
//        List<ViolateTilte> list = violateTilteService.selectViolateTilteList(violateTilte);
//
//        ExcelUtil<ViolateTilte> util = new ExcelUtil<ViolateTilte>(ViolateTilte.class);
//        util.exportExcel(response, list, entity.getFileName());

        violateFileService.exportToId(id,  request,  response);
    }
}
